﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;
using System.Linq;

namespace Strava1
{
    public class MeterToKMConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((float)value / 1000.0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MeterToFeetConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((float)value * 3.2808399);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MPSToMPHConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((float)value * 2.23693629);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class SecondsToTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int secs = (int)value;

            int mins = secs / 60;
            int seconds = secs - (mins * 60);

            return mins.ToString() + ":" + seconds.ToString("00");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class SpeedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return -1;
            /*Effort effort = (Effort)value;
            SegmentDetail sd;
            
            sd = (from s in App.ViewModel.Segments
                      where s.Id == effort.SegmentId
                      select s.Detail).FirstOrDefault();

            double distance = double.Parse(sd.distance.ToString());
            double seconds = double.Parse(effort.ElapsedTime.ToString());

            return 2.23693629 * distance / seconds;*/
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }



    public class CountMaxConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value >= 50)
            {
                return "50";
            }
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class JustIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int number;
            int.TryParse(value.ToString(), out number);
            if (number > 0) 
            {
                if (number == 5)
                {
                    return "HC";
                }
                return (5-number).ToString();
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RideSegmentPlacingsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string[] paramArr = parameter.ToString().Split('|');
            string type = paramArr[0];
            int targetPlacing = int.Parse(paramArr[1]);
            //int placing = 0;
            int placingCount = 0;

            Ride ride = (Ride)value;

            if (!ride.FullDetailsLoaded)
            {
                return -1;
            }

            /*switch (type) //Overall
            {
                case "Personal":
                    if ((ride.EffortSegments != null) && (ride.EffortSegments.Count > 0))
                    {
                        foreach (Segment segment in from e in ride.EffortSegments
                                                    select e.Segment)
                        {
                            if (segment.MyEfforts != null)
                            {
                                Effort rideEffort = (from e in segment.MyEfforts
                                                     where e.StartDate >= ride.Detail.StartDate &&
                                                     e.StartDate < ride.Detail.StartDate.AddSeconds(ride.Detail.ElapsedTime)
                                                     select e).FirstOrDefault();

                                //if (rideEffort.OverallPlacing == targetPlacing)
                                //{
                                //    placingCount++;
                                //}
                            }
                        }
                    }
                    break;
                case "Overall":
                    if ((ride.EffortSegments != null) && (ride.EffortSegments.Count > 0))
                    {
                        foreach (Segment segment in from e in ride.EffortSegments
                                                    select e.Segment)
                        {
                            if (segment.MyEfforts != null)
                            {
                                Effort rideEffort = (from e in segment.MyEfforts
                                                     where e.StartDate >= ride.Detail.StartDate &&
                                                     e.StartDate < ride.Detail.StartDate.AddSeconds(ride.Detail.ElapsedTime)
                                                     select e).FirstOrDefault();

                                int placing = 1;
                                //foreach (Effort effort in from e in segment.Efforts
                                //                          where e.Athlete.Username != App.ViewModel.Username
                                //                          select e)
                                //{
                                //    if (rideEffort.ElapsedTime < effort.ElapsedTime)
                                //    {
                                //        break;
                                //    }
                                //    placing++;
                                //}
                                if (placing == targetPlacing)
                                {
                                    placingCount++;
                                }
                            }
                        }
                    }
                    break;
            }*/

            return placingCount;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class SegmentStrokeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            PathGeometryObj pgo = (PathGeometryObj)value;
            if (pgo.MyPlacing == 0)
            {
                return new SolidColorBrush(Colors.Blue);
            }

            double overall = pgo.Overall;

            Color color = new Color();
            color.A = 255;
            color.R = 255;
            color.G = 0;
            color.B = 0;

            //double overall
            return new SolidColorBrush(color);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class TimeBehindLeaderConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int seconds = (int)value;
            if (seconds == 1000000)
            {
                return "";
            }
            double minutes = Math.Floor(seconds / 60);
            seconds = seconds % 60;
            if (minutes > 0)
            {
                return minutes.ToString() + "m " + seconds.ToString() + "s";
            }
            else
            {
                return seconds.ToString() + "s";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }



    public class textConv : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
